/**
 * 自定义订单
 * */




(function () {
})();


//购物车列表
function getList(){
    var formData = {
        //keyword: $('#keyword').val(),
        //phone: $('#phone').val(),
        //status: status,
        //start_day: $('#logmin').val(),
        //end_day: $('#logmax').val(),
        keyword: orderno,
        phone: phone,
        status: status,
        start_day: start_day,
        end_day: end_day,
        start: pages.start,
        delivery:delivery,
        pay_way:pay_way,
        limit: pages.limit
    };
    $.ajax({
        type: apis.orderIndex.method,
        url: apis.orderIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            formArys = res.data;
            $('#pageTotal').html(formArys.length);
            $('tbody').html(getHtml());
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//拼接字符串
function getHtml(){
    var d = formArys;
    var h = '', i, iscancel, delivery, status;
    for (i = 0; i < d.length; i ++) {
        iscancel = d[i].iscancel;
        delivery = d[i].delivery;
        status = d[i].status;
        h += '<tr class="text-c">';
        //h += '<td><input type="checkbox" onclick="selOne(' + d[i].id + ');"></td>';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].orderno + '</td>';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].phone + '</td>';
        h += '<td>' + d[i].gname + '</td>';
        h += '<td><img src="' + d[i].gimg + '" height="100"/></td>';
        h += '<td>' + d[i].sku_name + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>' + d[i].real_pay + '</td>';
        h += '<td>' + d[i].accepter + '</td>';
        h += '<td>' + d[i].acceptDoor + '</td>';
        h += '<td>';
        h += d[i].pay_way==1 ? '微信' : '余额';
        h += '</td>';
        h += '<td>' + d[i].level_name + '</td>';
        h += '<td>' + d[i].remark + '</td>';
        h += '<td class="f-14 statusDiv">';
        h += '<div>下单时间：' + d[i].addtime + '</div>';
        h += '<style type="text/css"> .statusBtn { margin: 10px 0; } .statusBtn a { margin-left: 10px; padding: 3px 10px; background: #0088cc; color: #fff; } </style>';
        h += '<div class="statusBtn">';
        h += '当前状态：' + d[i].status_name;
        if (iscancel == 2) {
            h += '<div style="height: 5px;"></div>';
            h += '<a href="javascript:;" onclick="shenhe_yes(' + d[i].id + ');">审核通过</a>';
            h += '<a href="javascript:;" onclick="shenhe_no(' + d[i].id + ');">审核拒绝</a>';
        }
        if (iscancel == 1 && delivery == 1) {
            if (status == 2) {
                //h += '<a href="javascript:;" onclick="send(' + d[i].id + ');">发货</a>';
                h += '<a href="javascript:;" onclick="order_send(\'发货\',\'order-send.html\', ' + d[i].id + ');" style="text-decoration: none">';
                h += '发货<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
            }
        }
        h += '</div>';
        h += orderStatus(d[i]);
        h += '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="order_show(\'详情\',\'order-show.html\', ' + d[i].id + ');">';
        h += '详情';
        h += '</a>';
        h += '<br>';
        h += '<a href="javascript:;" onclick="order_send(\'修改快递\',\'order-send.html\', ' + d[i].id + ');">';
        h += '修改快递';
        h += '</a>';
        if (d[i].status >= 3) {
            h += '<br>';
            h += '<a href="javascript:;" onclick="lay_tui(' + d[i].id + ');">';
            h += '退货审核';
            h += '</a>';
        }
        h += '<input type="hidden" id="wuliu' + d[i].id + '" value="' + d[i].wuliu + '"/>';
        h += '<input type="hidden" id="wuliuhao' + d[i].id + '" value="' + d[i].wuliuhao + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}
//订单状态
function orderStatus(d){
    var iscancel = d.iscancel;
    var delivery = d.delivery;
    var status = d.status;
    var h = '';
    h += '<style type="text/css"> .otab th, .otab td { padding: 2px 5px; padding-top: 5px; border: 1px solid #eee; } .otab th.oclick, .otab td.oclick { border: 1px solid #0088cc; } .otab div { margin-bottom: 5px; color: #fff; background: #ccc; text-align: center; cursor: pointer; } .otab div.light { background: #555; color: #eee; } </style>';
    h += '<table class="otab">';
    h += '<tr>';
    h += '<th class="oclick">关闭</th>';
    h += '<th class="oclick">快递</th>';
    h += '<th class="oclick">自提</th>';
    h += '</tr>';
    h += '<tr>';
    h += '<td valign="top" class="oclick">';
    h += iscancel==1 ? '<div class="light">正常</div>' : '<div>正常</div>';
    if (iscancel > 1) {
        h += iscancel>2 ? '<div class="light">已审核</div>' : '<div class="light">审核中</div>';
        h += iscancel==3 ? '<div class="light">已售后</div>' : '<div>售后</div>';
        //h += iscancel==4 ? '<div class="light">已拒绝</div>' : '<div>拒绝</div>';
    } else {
        h += '<div>审核中</div>';
        h += '<div>售后</div>';
        //h += '<div>拒绝</div>';
    }
    h += '</td>';
    if (iscancel == 1 && delivery == 1) {
        h += '<td valign="top" class="oclick">';
    } else if (iscancel == 1 && delivery == 2) {
        h += '<td valign="top" style="border-right: 1px solid #0088cc;">';
    } else {
        h += '<td valign="top">';
    }
    if (iscancel==1 && delivery==1) {
        if (status > 3) {
            h += '<div class="light">已付款</div><div class="light">已发货</div><div class="light">已收货</div><div class="light">已完成</div>';
        } else if (status > 2) {
            h += '<div class="light">已付款</div><div class="light">已发货</div><div class="light">未收货</div><div>未完成</div>';
        } else if (status > 1) {
            h += '<div class="light">已付款</div><div class="light">未发货</div><div>未收货</div><div>未完成</div>';
        } else {
            h += '<div class="light">未付款</div><div>未发货</div><div>未收货</div><div>未完成</div>';
        }
    } else {
        h += '<div>未付款</div>';
        h += '<div>未发货</div>';
        h += '<div>未收货</div>';
        h += '<div>未完成</div>';
    }
    h += '</td>';
    if (iscancel == 1 && delivery == 2) {
        h += '<td valign="top" class="oclick">';
    } else {
        h += '<td valign="top">';
    }
    if (iscancel == 1 && delivery == 2) {
        if (status > 2) {
            h += '<div class="light">已付款</div><div class="light">门店自提</div><div class="light">自提成功</div>';
        } else if (status >1) {
            h += '<div class="light">已付款</div><div class="light">门店自提</div><div>自提成功</div>';
        } else {
            h += '<div class="light">未付款</div><div>门店自提</div><div>自提成功</div>';
        }
    } else {
        h += '<div>未付款</div>';
        h += '<div>门店自提</div>';
        h += '<div>自提成功</div>';
    }
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    return h;
}


//审核通过弹窗
function shenhe_yes(v){
    var msg = '确认要 <b style="color: #0088CC; font-size: 20px;">通过审核</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        shenhe({id: v, status: 2});
    });
}
//审核拒绝弹窗
function shenhe_no(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">拒绝审核</b> 吗？';
    layer.confirm(msg,function(index){
        shenhe({id: v, status: 1});
    });
}
//审核弹窗
function shenhe(e){
    $.ajax({
        type: apis.orderAuth.method,
        url: apis.orderAuth.url,
        data: { id: e.id, status: e.status },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg);
                return
            }
            layerMsgOk('操作成功');
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//退货审核
function lay_tui(v){
    var msg = '确认要 <b style="color: #0088CC; font-size: 20px;">同意退货</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.orderTuiAuth.method,
            url: apis.orderTuiAuth.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg);
                    return
                }
                layerMsgOk('操作成功');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


////发货
//function send(v){
//    var msg = '确认要 <b style="color: #0088CC; font-size: 20px;">确认发货</b> 吗？';
//    layer.confirm(msg,function(index){
//        //$(obj).parents("tr").remove();
//        //console.log(id, index);
//        $.ajax({
//            type: apis.orderSend.method,
//            url: apis.orderSend.url,
//            data: { id: v },
//            success: function (res) {
//                console.log(res);
//                if (res.code < 0) {
//                    layerMsgErr(res.msg);
//                    return
//                }
//                layerMsgOk('操作成功');
//                setTimeout(function(){
//                    top.location.reload();
//                }, 1000);
//            },
//            error: function () {
//                console.log('错误了！');
//            }
//        });
//    });
//}


//详情弹窗
function order_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    $.ajax({
        type: apis.orderOne.method,
        url: apis.orderOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#orderno').html(d.orderno);
            $('#buyer').html(d.uname);
            $('#gname').html(d.gname);
            var gimg = d.gimg=='' ? '-' : '<img src="' + d.gimg + '"/>';
            $('#gimg').html(gimg);
            $('#sku_name').html(d.sku_name);
            $('#sku_price').html(d.price + '元');
            $('#coupon').html(d.quata + '元');
            $('#realPay').html(d.real_pay + '元');
            $('#sku_num').html(d.num);
            $('#delivery').html(d.deliveryName);
            $('#accepter').html(d.accepter);
            $('#acceptName').html(d.acceptName);
            $('#acceptPhone').html(d.acceptPhone);
            $('#wuliu').html(d.wuliu);
            $('#wuliuhao').html(d.wuliuhao);
            $('#remark').val(d.remark);
            $('#addtime').html(d.addtime);
            $('#paytime').html(d.paytime);
            $('#sendtime').html(d.sendtime?d.sendtime:'-');
            $('#finishtime').html(d.finishtime?d.finishtime:'-');
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//发货弹窗
function order_send(title,url,id,w,h){
    url += '?id=' + id;
    var wuliu = $('#wuliu' + id).val();
    var wuliuhao = $('#wuliuhao' + id).val();
    url += '&wuliu=' + escape(wuliu);
    url += '&wuliuhao=' + escape(wuliuhao);
    layer_show(title,url,w,300);
}


//发货
function send(){
    var formData = {
        id: id,
        wuliu: $('#wuliu').val(),
        wuliuhao: $('#wuliuhao').val(),
    };
    //console.log(formData); return
    $.ajax({
        type: apis.orderSend.method,
        url: apis.orderSend.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                //window.location.href = '';
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//发货弹窗
function batch_send(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,500);
}

//获取批量订单
function getSendnoList(){
    var formData = {
        status: 3,
        start: 0, limit: 100,
    };
    $.ajax({
        type: apis.orderIndex.method,
        url: apis.orderIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i, idAry = [];
            for (i = 0; i < d.length; i ++) {
                status = d[i].status;
                h += '<tr class="text-c">';
                h += '<td id="orderno-' + d[i].id + '">' + d[i].orderno + '</td>';
                h += '<td>' + d[i].accepter + '</td>';
                h += '<td>' + d[i].acceptDoor + '</td>';
                h += '<td><input type="text" class="input-text" id="wuliu-' + d[i].id + '" value="' + d[i].wuliu + '"/></td>';
                h += '<td><input type="text" class="input-text" id="wuliuhao-' + d[i].id + '" value="' + d[i].wuliuhao + '"/></td>';
                h += '</tr>';
                idAry.push(d[i].id);
            }
            $('tbody').html(h);
            $('#ids')[0].value = idAry.join(',')
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//批量发货
function batchSend(){
    var ids = $('#ids').val();
    if (ids.length ==0) {
        msgPop({msg: '没有记录'}); return
    }
    ids = ids.split(',');
    var os = [], i, id, orderno, wuliu, wuliuhao;
    for (i = 0; i < ids.length; i ++) {
        id = ids[i];
        orderno = $('#orderno-' + id).html();
        wuliu = $('#wuliu-' + id).val();
        wuliuhao = $('#wuliuhao-' + id).val();
        if (wuliu && !wuliuhao) {
            msgPop({msg: orderno + '的快递名称没填'}); return
        }
        if (!wuliu && wuliuhao) {
            msgPop({msg: orderno + '的快单号没填'}); return
        }
        console.log(wuliu, wuliuhao);
        if (wuliu && wuliuhao) {
            os.push({
                id: id, orderno: orderno,
                wuliu: wuliu, wuliuhao: wuliuhao,
            });
        }
    }
    var formData = {os: os};
    console.log(ids, os, formData);
    $.ajax({
        type: apis.orderBatchSend.method,
        url: apis.orderBatchSend.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                //window.location.href = '';
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//导出
function daochu(){
    var formData = {};
    $.ajax({
        type: apis.orderOut.method,
        url: apis.orderOut.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //ms
            $('#down').html('<a href="' + res.data + '" target="_blank" class="btn btn-primary radius">下载数据</a>');
        },
        error: function () {
            console.log('错误辣');
        }
    });
}